package io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SimpleParser {
	
	public static List<GPSPoint> parseFile(final File file) throws Exception {
		BufferedReader r = new BufferedReader(new FileReader(file));
		String line = null;
		SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
		int timePoints = 0;
		long start = 0;
		double sum = 0;
		Calendar cal = Calendar.getInstance();
		List<GPSPoint> ret = new ArrayList<GPSPoint>();
		//371.0	47.54711115812221	7.583094013431431	400.0	04.10.2011 13:22:58	gps
		while ((line = r.readLine()) != null) {
			line = line.replace("", "");
			String c[] = line.split("\t");
			float lat = Float.parseFloat(c[2]);
			float lon = Float.parseFloat(c[1]);
			float ele = Float.parseFloat(c[0]);
			float pre = Float.parseFloat(c[3]);
			String time = c[4];
			float speed = -1;
			float bearing = -400;
			if (c.length > 6) {
				speed = Float.parseFloat(c[5]);
			}
			if (c.length > 7) {
				bearing = Float.parseFloat(c[6]);
			}
			long cT = sd.parse(time).getTime();
			ret.add(new GPSPoint(cT, lat, lon, ele, pre, speed, bearing));
			timePoints++;
		}
		System.err.println("Total time (min)" + sum / 60.0 + " number of timepoints " + timePoints);
		return ret;
	}
	
}
